import type { PageType } from './page_handler/type'
import { AppStyleCollections } from '../types/constants'
import { BaiduPage } from './page_handler/platform/baidu'
import { BaiduPageType } from './page_handler/platform/baidu/types'
import { GooglePage } from './page_handler/platform/google'
import { ToolKit } from './utils/toolkit'

export class MarkElement {
  private static instance: MarkElement

  public static getInstance(): MarkElement {
    if (!MarkElement.instance) {
      MarkElement.instance = new MarkElement()
    }
    return MarkElement.instance
  }

  /**
   * 标记百度搜索元素
   */
  markBaiduElements(pageType: PageType): void {
    const selectors = BaiduPage.getInstance().getResultSelectors()
    const { selector } = selectors

    if ([BaiduPageType.Web, BaiduPageType.News].includes(pageType as BaiduPageType)) {
      ToolKit.browser.platform.markElement($(selector), AppStyleCollections.Search.class.SEARCH_RESULT_ITEMS)

      const $contentNone = $('#wrapper #wrapper_wrapper #container .content_none')
      ToolKit.browser.platform.markElement($contentNone.find('p:contains("提示：")'), AppStyleCollections.Search.class.SEARCH_TIP)
      ToolKit.browser.platform.markElement($contentNone.find('p:contains("抱歉，未找到相关结果")'), AppStyleCollections.Search.class.SEARCH_NO_RESULT_TIP)
    }
  }

  /**
   * 标记 Google 搜索元素
   */
  markGoogleElements(): void {
    const selectors = GooglePage.getInstance().getResultSelectors()
    const { selector } = selectors

    // 标识结果链接
    ToolKit.browser.platform.markElement(
      $(selector).find('h3 + div:has(cite)'),
      AppStyleCollections.Google.LINK,
    )
    // 标识结果链接详细按钮
    ToolKit.browser.platform.markElement(
      $(selector).find('h3').closest('span').siblings('div'),
      AppStyleCollections.Google.LINK_DETAIL_BUTTON,
    )
  }
}
